import { API_LIST } from '../../../Api/Api'
import { commonTableList, BuiltInData } from '../Common/common'
import {
    AddDataButton,
    ModDataButton,
    DelDataButton,
    CommonButton,
    AnnexButton,
    ExecuteButton,
    OpenTwoRouter,
    InputButton,
} from '../Common/options'
import { InfoTypeManageAppoint, ProjectList, getOrgTree } from '../Common/Conversion'

import { project } from '../../../Data'

export const InfoManage = async ({ uuid, url, OptionsEvent, ClientData, Permission, actionRef }) => {
    const { hostname } = window.location
    const { client_id } = project[hostname]
    let data = {
        pageSize: 10,
        params: { pageIndex: 1, InfoTypeManageUuid: uuid, OEventDate: `asc@8`, OSortId: 'asc@9' },
        get: API_LIST()[`get${url}`],
        added: `pos${url}`,
        modify: `put${url}`,
        delete: `del${url}`,
        options: [<AddDataButton OptionsEvent={OptionsEvent} type="primary" icon="add" title="新增" />],
        columns: [
            commonTableList.uuid,
            { ...commonTableList.SearchSort, selectType: 'InfoManage' },
            { ...commonTableList.tableName, title: '主题', width: 400, ellipsis: true },
            {
                dataIndex: 'infoTypeManageUuid',
                title: '信息类型',
                valueType: 'treeSelect',
                fieldProps: {
                    options: await InfoTypeManageAppoint(uuid),
                    fieldNames: { label: 'tableName', value: 'uuid' },
                },
                ellipsis: true,
                // hideInSearch: true,
                rules: true,
                width: 300,
            },
            {
                ...commonTableList.projectUuid,
                valueEnum: await ProjectList(),
                hideInAdded: true,
                hideInModify: true,
                hideInTable: true,
                hideInSearch: !hostname.includes('jszx'),
            },
            {
                dataIndex: 'organizationUuid',
                title: '组织机构',
                valueType: 'treeSelect',
                fieldProps: {
                    options: await getOrgTree(),
                    fieldNames: { label: 'tableName', value: 'uuid' },
                },
                ellipsis: true,
                rules: true,
                width: 300,
            },
            {
                dataIndex: 'approvalStatus',
                title: '审核状态',
                valueType: 'select',
                valueEnum: BuiltInData.ReviewStatus,
                hideInAdded: true,
                hideInModify: true,
                width: 100,
            },
            {
                dataIndex: 'eventDate',
                title: '日期',
                valueType: 'Date',
                width: 200,
                hideInSearch: true,
            },
            commonTableList.sortId,
            {
                dataIndex: 'coverAttachmentUuid',
                title: '文件封面',
                valueType: 'upload',
                valueFile: [1],
                fileUrl: 'coverUrl',
                hideInSearch: true,
                hideInTable: true,
            },
            {
                dataIndex: 'coverUrl',
                title: '文件封面',
                valueType: 'hidden',
                hideInSearch: true,
                hideInTable: true,
                width: 400,
            },
            commonTableList.remark,
            commonTableList.StartDate,
            commonTableList.EndDate,
            commonTableList.createUserName,
            commonTableList.createTime,
            commonTableList.updateUserName,
            commonTableList.updateTime,
            {
                title: '操作',
                valueType: 'systemOperation',
                fixed: 'right',
                width: 400,
                hideInSearch: true,
                hideInAdded: true,
                hideInModify: true,
                render: (_, record) => {
                    const { approvalStatus, uuid: itemUuid, fileType, coverUrl } = record
                    return [
                        approvalStatus === 0 && (
                            <ExecuteButton
                                actionRef={actionRef}
                                key="zero"
                                title="提交审核"
                                url="posInfoManageSubmit"
                                path={[itemUuid]}
                                tips="数据已提交审核中!"
                            />
                        ),
                        approvalStatus === 1 && (
                            <ExecuteButton
                                actionRef={actionRef}
                                key="one"
                                title="合格"
                                url="posInfoManageApproval"
                                path={[itemUuid, 2]}
                                tips="文件通过审核!"
                            />
                        ),
                        approvalStatus === 1 && (
                            <ExecuteButton
                                actionRef={actionRef}
                                key="tow"
                                title="不合格"
                                danger={true}
                                url="posInfoManageApproval"
                                path={[itemUuid, 3]}
                                tips="文件未通过审核!"
                            />
                        ),
                        (approvalStatus === 2 || approvalStatus === 3) && (
                            <ExecuteButton
                                actionRef={actionRef}
                                key="thr"
                                title="重置状态"
                                danger={true}
                                url="posInfoManageApproval"
                                path={[itemUuid, 0]}
                                tips="数据状态已重置!"
                            />
                        ),
                        approvalStatus === 3 && (
                            <ExecuteButton
                                actionRef={actionRef}
                                key="fou"
                                title="重新提交"
                                danger={true}
                                url="posInfoManageAccept"
                                path={[itemUuid]}
                                tips="重新提交文件!"
                            />
                        ),
                        (fileType === 0 || fileType === 2) && (
                            <OpenTwoRouter
                                key="att"
                                title="附件列表"
                                modal="TableList"
                                OptionsEvent={OptionsEvent}
                                other={{
                                    page: 'FileManagement',
                                    Permission,
                                    oneData: record,
                                    header: data.columns,
                                }}
                            />
                        ),
                        (fileType === 1 || fileType === 2) && (
                            <CommonButton
                                key="RichText"
                                OptionsEvent={OptionsEvent}
                                title="编辑富文本"
                                data={{ type: 'RichTextEditor', record, width: 1500 }}
                            />
                        ),
                        fileType === 3 && (
                            <InputButton
                                key="link"
                                OptionsEvent={OptionsEvent}
                                title="链接地址"
                                data={{
                                    record,
                                    actionRef,
                                    label: '链接',
                                    smallTitle: '文件链接地址',
                                    url: 'putInfoManage',
                                    field: 'currencyA',
                                }}
                            />
                        ),
                        coverUrl && (
                            <AnnexButton OptionsEvent={OptionsEvent} key="see" url={coverUrl} title="文件封面" />
                        ),
                        <ModDataButton OptionsEvent={OptionsEvent} key="modify" title="修改" data={record} />,
                        <DelDataButton OptionsEvent={OptionsEvent} key="del" title="删除" url={url} uuid={itemUuid} />,
                    ]
                },
            },
        ],
    }
    return data
}
